﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Windows;
using SSATool.BL.DM;
using SSATool.Common.Logging;
using SSATool.DL.Client;
using SSATool.UI.Common.Threading;
using SSATool.UI.View.Bootstrap.Properties;
using SSATool.UI.ViewModel;

#endregion

namespace SSATool.UI.View.Bootstrap
{
    /// <summary>
    /// Contains the bootstrapping facilities of the current application.
    /// In a future release, this may happen with a dependency injenction
    /// framework such as Unity / Spring.NET.
    /// </summary>
    public class Bootstrapper
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="Bootstrapper"/> class.
        /// </summary>
        public Bootstrapper()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        public BootstrapContext Load()
        {
            BootstrapContext bootstrapContext = new BootstrapContext();

            InitializeLogging();
            InitializeViewModel(bootstrapContext);
            
            return bootstrapContext;
        }

        private void InitializeLogging()
        {
            Logger<Bootstrapper>.Info(Resources.LogInitializeLogging);
            {
                LoggerConfig.RegisterConfigFile("Logging/LoggingConfig.xml");
            }
            Logger<Bootstrapper>.Info(Resources.LogInitializeLoggingCompleted);
        }

        private void InitializeViewModel(BootstrapContext bootstrapContext)
        {
            Logger<Bootstrapper>.Info(Resources.LogInitializeViewModel);
            {
                SyncDispatcher dispatcher = new SyncDispatcher(Application.Current.Dispatcher);
                ServiceWcfClient dataAccess = new ServiceWcfClient(dispatcher);
                DomainContext domainContext = new DomainContext(dataAccess);
                DataContext dataContext = new DataContext(domainContext);

                bootstrapContext.Dispatcher = dispatcher;
                bootstrapContext.DataContext = dataContext;
            }
            Logger<Bootstrapper>.Info(Resources.LogInitializeViewModelCompleted);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
